<?php
require('main.php');


class Solution extends Main {
	
	function Solution()
	{
		parent::Main();
		$this->lib('message');
		$this->database();	
		$this->lib('pagination'); //分页;
		
	}
	
	function show()
	{
		$this->my_view("manage/solution/solution",$this->solutionList());
	}
	
	function init_add()
	{
		$this->my_view("manage/solution/solution_add",$this->languageList() );
	}
	
	/**
	 * 
	 * 添加案例；
	 * @return unknown_type
	 */
	function add()
	{
		
		$result = $this->db->insert('solution',$this->_fetch_form() );
		
		if($result) {
			
			$this->message->set("恭喜你,添加方案成功!");
			$this->my_view('manage/solution/solution_success');
		}
		else {
            
            $this->message->set("抱歉,添加方案失败！");
			$this->my_view('manage/solution/solution_add',$this->languageList());
		}
		
		
	}
	
	/**
	 * 
	 * 案例的删除。。
	 * @param $id
	 * @return unknown_type
	 */
	
	function delete()
	{ 
	   $id_sql = join(",",$_POST['id'] );
	   $result =  $this->db->delete('solution', "id in($id_sql)" );
	   
	   if($result)
	   {
		       $this->message->set("恭喜你,删除方案成功！");
		       $this->my_view('manage/solution/solution',$this->solutionList() );
       }
	   else
	   {
		       $this->message->set("抱歉,删除方案失败！");
		       $this->my_view('manage/solution/solution',$this->solutionList() ); 	
		 	  	
	   }
	}
	
	/**
	 * 
	 * 案例的修改；
	 * 
	 * 
	 */
	function  init_update($id)
	{ 
		 $language_list = $this->db->find("select * from languages");  
		 
         $solution_id_row =  $this->db->find("select * from solution where id= $id" ); 
         
         $data = array(
                 'language_list' =>$language_list,
                 'solution_id_row' =>$solution_id_row
         );

         $this->my_view('manage/solution/solution_update',$data);
        
	}
	
	/**
	 * 
	 * 
	 * @文章更新成功的方法
	 * 调用了articleAdd() 方法
	 * 
	 * 
	 */
	function update()
	{  
		 $language_list = $this->db->find("select * from languages"); 
		 $solution_id_row =  $this->db->find("select * from solution where id=".$_POST['id'] ); 
         
         $data = array(
                 'language_list' =>$language_list,
                 'solution_id_row' =>$solution_id_row
         );	
         
         
		$result = $this->db->update('solution', $this->_fetch_form(), 'id=' . $_POST['id']);
		if($result ){
			 $this->message->set("恭喜你,修改方案成功！");
			 $this->my_view('manage/solution/solution',$this->solutionList() ); 
		}
		else{
		     $this->message->set("抱歉,修改方案失败！");
		     $this->my_view('manage/solution/solution_update',$data); 	
			
		}
		
	}
	
	
	
	
    /**
     * 
     * 
     * 方案列表。。。
     * @return unknown_type
     */	
	function solutionList()
	{
         $total = $this->db->count('solution');
         $config = array(
         'total_row' =>$total
         );
         $this->pagination->init($config);
         $solution_list = $this->db->find("select * from solution order by created desc limit "
                          .$this->pagination->start_row.",10");
                          
          return array(
         'solution_list' =>$solution_list
         );
	}
	
	/**
	 * 语言列表；
	 * @return unknown_type
	 */
	
	function languageList()
	{
			$language_list = $this->db->find("select * from languages");
		    return array(
		        'language_list'=>$language_list
		    );
	}
	
	/**
	 * 
	 * 案例插入。。。
	 * 
	 */
	function _fetch_form() 
	{
        return array(
        'language'		=>$_POST['language'],
        'name'			=>$_POST['name'],
        'title'			=>$_POST['title'],
        'content'		=>$_POST['summary'],
        'url_pattern'	=>$_POST['url_pattern'],   
        'keywords'		=>$_POST['keywords'],  
        'description'	=>$_POST['description']
        );
	}
	
	
	
	
}
?>